<template>
  <div>
    <van-nav-bar
    title="注册"
    left-arrow
    @click-left="onClickLeft"
    />
    <!-- {{$store.state.a}} -->
    <van-form @submit="onSubmit" ref="regFrom">
      <van-field
        v-model="mobile"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="regular.username"
      />
     
      <van-field
        v-model="sms"
        center
        clearable
        label="短信验证码"
        placeholder="请输入短信验证码"
        :rules="regular.authcode"
        >
        <template #button native-type="button">
            <van-button  size="small" type="primary" @click="getfasyzm" native-type="button">发送验证码</van-button>
        </template>

    </van-field>
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="regular.password"
    />
      <div style="margin: 16px">
        <van-button round block type="info" native-type="submit" @click="getreg">提交</van-button>
      </div>
    </van-form>



  </div>
</template>

<script>
import { Form } from "vant";
import { Field } from "vant";
import regular from '@/utils/verify/regular'
export default {
  name: "WorkspaceJsonLogin",
  components: {
    [Form.name]: Form,
    [Field.name]: Field,
  },
  data() {
    return {
        regular,
        mobile:'',
        sms:'',
        password:'',
         
    };
  },

  mounted() {},

  methods: {
     showPopup() {
      this.show = true;
    },
    onSubmit(values) {
      console.log('submit', values);
    },
    // 返回上一级
    onClickLeft(){
        this.$router.go(-1)
    },
    // 发送验证码
    getfasyzm(){
      this.$refs.regFrom.validate('username').then(res=>{
        let {mobile}=this
        this.$http.post('/user/sendSms',{
            mobile:mobile,
            type:'register'
        })
        .then(res=>{
        
        console.log(res);

        })
      }).catch(err=>{
        console.log(err);
      })
      
    },
    // 注册
    getreg(){
        let {mobile,password,sms}=this
        this.$http.post('/user/reg',{
            username:mobile,
            password:password,
            vercode:sms
        })
        .then(res=>{
          console.log(res);
          this.$toast(res.msg)
          setTimeout(()=>{
              this.$router.go(-1);
          },800)
        })
    }
  },
};
</script>

<style lang="scss" scoped>
</style>